package mahmoud.sigir.parse;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashSet;
import java.util.TreeMap;


import mahmoud.utils.QueryPair;

public class OhsumedQueryParser
{

	private String queryFileLocation;
	private String evaluationFile;
	public  TreeMap<Integer, QueryPair> StoredQueries;
	public  TreeMap<Integer, HashSet<String> > QueryRelevantDocuments;
	
	public OhsumedQueryParser(String qLoc, String evalFile)
	{
		queryFileLocation=qLoc;
		evaluationFile = evalFile;
	}
	public  TreeMap<Integer, QueryPair> parseQueryFile() 
	{
		String sn =  null, patientinfo = null, query=null; 
		StoredQueries = new TreeMap<Integer, QueryPair>();
		try 
		{
			//use buffering, reading one line at a time
			//FileReader always assumes default encoding is OK!
			BufferedReader BR =  new BufferedReader(new FileReader(queryFileLocation));
			String line=null, preline = null; //not declared within while loop
			/*
			 * readLine is a bit quirky :
			 * it returns the content of a line MINUS the newline.
			 * it returns null only for the END of the stream.
			 * it returns an empty String if two newlines appear in a row.
			 */
			while (( line = BR.readLine()) != null)
			{
				if(line.contains(".I"))
				{
					if(query!= null & patientinfo!=null)
						this.StoredQueries.put(Integer.parseInt(sn), new QueryPair(sn,query,patientinfo));
					sn= line.replace(".I", "").trim();
				}
				if(preline!= null && preline.contains(".B"))
					patientinfo=(line);
				if(preline!= null && preline.contains(".W"))
					query = line;
				preline =line;
			}
			this.StoredQueries.put(Integer.parseInt(sn), new QueryPair(sn,query,patientinfo));
			BR.close();
			return this.StoredQueries;
		}
		catch (IOException ex)
		{
			ex.printStackTrace();
			return null;
		}
	}
	public  TreeMap<Integer, HashSet<String> > parseEvalFile() 
	{
		QueryRelevantDocuments = new TreeMap<Integer, HashSet<String> >();
		try 
		{
			BufferedReader BR =  new BufferedReader(new FileReader(evaluationFile));
			String line=null; 
			while (( line = BR.readLine()) != null)
			{
				String qid, docid;
				HashSet<String> Temp = new HashSet<String>() ;
				String[] x = line.split("\t");
				qid = x[0]; docid= x[1];
				int iqid = Integer.parseInt(qid);
				if(QueryRelevantDocuments.containsKey(iqid))
				{
					Temp = QueryRelevantDocuments.get(iqid);
				}
				Temp.add(docid);
				QueryRelevantDocuments.put(iqid, Temp);

			}
			BR.close();
			return QueryRelevantDocuments;
		}
		catch (IOException ex)
		{
			ex.printStackTrace();
			return null;
		}
	}
	public static void main(String[] args) 
	{
		OhsumedQueryParser OQP = new OhsumedQueryParser("/media/Data/dDocuments/Code/ohsumed/misc/queries.txt", "/media/Data/dDocuments/Code/ohsumed/misc/drel.i");
		TreeMap<Integer, QueryPair> queries = OQP.parseQueryFile();
		while(!queries.isEmpty())
			System.out.println(queries.pollFirstEntry().getValue().Query);
//		OQP.
		

	}

}
